import React from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  TouchableHighlight,
  Image,
} from 'react-native';

import CommonViewPagerMultiShow from '../common/CommonViewPagerMultiShow';
import TImage from '../../components/TImage';
import SizeUtils from '../../common/SizeUtils';
import ImageConstants from '../../common/ImageConstants';
import CommonStyles from '../../common/CommonStyles';
import NewsConstants from '../../common/NewsConstants';
import Constants from '../../common/Constants';
import Config from '../../config';
import CommonDataListView from '../common/CommonDataListView';
import NewsListItemView from './NewsListItemView';

import RequestNewsPostsAction from '../../actions/news/RequestNewsPostsAction';

export default class NewsTypeShowView extends CommonDataListView {
  constructor(props) {
    super(props);

    this._viewPager = null;
    this._initComplete = this.props.autoInitRequest === true;
    this._dataKey = "news_id";
  }

  _dealComponentWillUpdate(nextProps, nextState) {
    super._dealComponentWillUpdate(nextProps, nextState);

    if(this._initComplete === false && nextProps.isCurrent === true) {
      this._initComplete = true;
      this.doRefresh();
    }
  }
  ddRefresh(){
    this.doRefresh();
  }
  _onRenderItem(data, index) {
    return (<NewsListItemView dRefresh={this.ddRefresh.bind(this)} type={this.props.type} data={data} />);
  }

  _onRenderHeader() {
    if(this.props.type == NewsConstants.type_daqing || this.props.type == NewsConstants.type_yaowen || this.props.type == NewsConstants.type_all) {
      return this._renderViewPager();
    }
    return (
      //要不然不显示下拉刷新
      <View style={{height:1}}>
      </View>
    )
  }

  _renderViewPager() {
    let pagerType = null;
    if(this.props.type == NewsConstants.type_daqing||this.props.type == NewsConstants.type_all) {
      pagerType = Constants.SLIDER_TYPE.NEWS_YAOWEN;
    }
    else {
      pagerType = Constants.SLIDER_TYPE.NEWS_DAQING;
    }
    return (
      <View style={{width:this.props.style.width,
          height:SizeUtils.translateDesignWidthToCode(356),
          borderBottomWidth:SizeUtils.translateDesignNumToCode(3), borderColor:"#eeeeee"}}>
        <CommonViewPagerMultiShow home={this.props.home} ref={(ref) => this._setViewPagerRef(ref)}
          type={pagerType} />
      </View>
    )
  }



  _setViewPagerRef(ref) {
    if(this._viewPager == null && ref != null) {
      this._viewPager = ref;
    }
  }

  _requestData(page) {
    new RequestNewsPostsAction().call(this, {page:page, tag:this.props.type})
    if(this._viewPager) {
      this._viewPager.doRefresh();
    }
  }
}
